<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>攻略</title>
    <!--引入vue.js库-->
    <!-- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> -->
    <script src="/js/vue.js"></script>
    <!-- element-ui引入样式 -->
    <link rel="stylesheet" href="/css/index.css">
    <!-- element-ui引入组件库 -->
    <script src="/js/index.js"></script>
    <!--引入axios库-->
    <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script>-->
    <script src="/js/axios.min.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body>
<div id="app">
    <!-- 根据参数查询 和 添加数据 -->
    <el-row>
        <el-col :span="4">
            <el-input v-model="searchForm.titleA" placeholder="请输入景区"></el-input>
        </el-col>
        <el-col :span="8">
            <el-button type="success" icon="el-icon-search" @click="search">搜索</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加</el-button>
            <el-button type="primary" icon="el-icon-refresh" @click="refresh">刷新</el-button>
        </el-col>
        <el-col :span="12"></el-col>
    </el-row>

    <!--    查询-->
    <el-table :data="tableData" border style="width: 100%" >
        <el-table-column width="90" prop="id" label="标题"></el-table-column>
        <el-table-column width="90" prop="title" label="标题"></el-table-column>
        <el-table-column label="封面" width="120">
                    <template slot-scope="scope">
                        <el-image
                                style="width: 96px; height: 84px"
                                :src="scope.row.gcover"
                        ></el-image>
                    </template>
                </el-table-column>
        <el-table-column width="120" prop="areaName" label="地域代码"></el-table-column>
        <el-table-column width="120" prop="scenicname" label="景区"></el-table-column>
        <el-table-column width="120" prop="chanName" label="栏目代码"></el-table-column>
        <el-table-column width="390" prop="summary" label="简述"></el-table-column>
        <el-table-column width="135" prop="ispic" label="是否包含图片" :formatter="getStatus"></el-table-column>
        <el-table-column width="180" prop="gdoc" label="文档"></el-table-column>
        <el-table-column width="99" prop="cjrid" label="创建者"></el-table-column>
        <el-table-column width="201" prop="cjtime" label="创建时间"></el-table-column>
        <el-table-column width="99" prop="updaterid" label="更新人姓名"></el-table-column>
        <el-table-column width="201" prop="updatetime" label="更新时间"></el-table-column>
        <el-table-column width="99" prop="dnum" label="下载数"></el-table-column>
        <el-table-column width="200" prop="weight" label="推荐度"></el-table-column>
        <el-table-column width="180" prop="gfeature" label="特色"></el-table-column>
        <el-table-column width="180" prop="gmap" label="地图"></el-table-column>
        <el-table-column width="180" prop="gscenic" label="景区"></el-table-column>
        <el-table-column width="180" prop="ghotel" label="住宿"></el-table-column>
        <el-table-column width="180" prop="gdine" label="餐饮"></el-table-column>
        <el-table-column width="180" prop="gfun" label="娱乐"></el-table-column>
        <el-table-column width="180" prop="gbuy" label="购物"></el-table-column>
        <el-table-column width="180" prop="groute" label="线路推荐"></el-table-column>
        <el-table-column width="180" prop="gtraffic" label="交通"></el-table-column>
        <el-table-column width="180" prop="gpass" label="旅行路上"></el-table-column>
        <el-table-column fixed="right" width="300" label="操作" width="140" align="center">
            <template slot-scope="scope">
                <el-button
                        type="primary"
                        @click="handleEdit(scope.$index, scope.row)">编辑
                </el-button>
                <el-button type="danger" @click="deleteDept(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!-- 分页功能 -->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[6, 12, 24, 120]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
    <!--    添加修改弹窗-->
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" ref="form">
            <el-form-item label="标题" :label-width="formLabelWidth" >
                <el-input v-model="form.title" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="封面" :label-width="formLabelWidth" >
                <!--上传图片  -->
                <el-upload
                        class="avatar-uploader"
                        action="/user/uploadFile"
                        name="file1"
                        :show-file-list="false"
                        :on-success="handleFileSuccess"
                        :before-upload="beforeFileUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="地域代码" :label-width="formLabelWidth" >
                <template>
                    <el-select v-model="form.areacode" placeholder="地域代码">
                        <el-option
                                v-for="item in options1"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item label="景区id" :label-width="formLabelWidth" >
                <template>
                    <el-select v-model="form.scenicid" placeholder="景区代码">
                        <el-option
                                v-for="item in options2"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item label="栏目代码" :label-width="formLabelWidth" >
                <template>
                    <el-select v-model="form.channelcode" placeholder="栏目代码">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </template>
            </el-form-item>
            <el-form-item label="简述" :label-width="formLabelWidth" >
                <el-input v-model="form.summary" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="是否包含图片" :label-width="formLabelWidth" >
                <el-select v-model="form.ispic" placeholder="请选择是否包含图片">
                    <el-option label="包含" value="0"></el-option>
                    <el-option label="不包含" value="1"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="文档" :label-width="formLabelWidth" >
                <el-input v-model="form.gdoc" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="推荐度" :label-width="formLabelWidth" >
                <el-input v-model="form.weight" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="特色" :label-width="formLabelWidth" >
                <el-input v-model="form.gfeature" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="住宿" :label-width="formLabelWidth" prop="password">
                <el-input v-model="form.ghotel" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="餐饮" :label-width="formLabelWidth" prop="password">
                <el-input v-model="form.gdine" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="娱乐" :label-width="formLabelWidth" prop="password">
                <el-input v-model="form.gfun" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="购物" :label-width="formLabelWidth" prop="password">
                <el-input v-model="form.gbuy" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="线路推荐" :label-width="formLabelWidth" prop="password">
                <el-input v-model="form.groute" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="交通" :label-width="formLabelWidth" prop="password">
                <el-input v-model="form.gtraffic" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="旅行路上" :label-width="formLabelWidth" prop="password">
            <el-input v-model="form.gpass" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addOrUpdate">确 定</el-button>
        </div>
    </el-dialog>

</div>
<script>
    var vue = new Vue({
        el: '#app',
        data() {
            return {
                imageUrl:'',//封面照片
                options:[],//攻略栏目
                options1:[],//地域栏目
                options2:[],//景区栏目
                //表格数据
                tableData: [],//返回的查询数据
                currentPage: 1,//页码
                pageSize: 6,//每页显示的数量
                total: 100,//分页总数量
                searchForm: {titleA: ''},//查询携带的参数
                dialogFormVisible: false, //使弹出不显示
                title: '攻略添加', //title 默认为 攻略添加
                //添加或者更新数据时 携带的参数
                form: {
                    title: '',
                    gcover: '',
                    areacode: '',
                    scenicid: '',
                    channelcode: '',
                    summary: '',
                    ispic: '',
                    gdoc: '',
                    weight: '',
                    gfeature: '',
                    gmap: '',
                    gscenic: '',
                    ghotel: '',
                    gdine: '',
                    gfun: '',
                    gbuy: '',
                    groute: '',
                    gtraffic: '',
                    gpass: ''
                },
                //弹出框的宽度
                formLabelWidth: '120px',
                rules: {
                    title: [{required: true, message: '请输入标题', trigger: 'blur'}],
                    // province: [{required: true, message: '请输入部门位置', trigger: 'blur'}],
                }
            }
        },
        //加载时默认执行的方法
        created: function () {
            //调用封装的方法
            this.loadData();//分域查询
            this.querySceson();//栏目
            this.queryArea();//地域
            this.querySenic();//景区
        },
        methods: {//定义变量_this等于外面的this
            //封装请求数据的方法
            loadData() {
                var _this = this;
                axios.post('/car/queryPage', {
                    pageNo: this.currentPage,
                    pageSize: this.pageSize,
                    data: {
                        "title": this.searchForm.titleA
                    }
                })
                    .then(function (response) {
                        console.log(response);
                        //判断是否执行成功
                        if (response.data.code == 200) {
                            _this.tableData = response.data.data.list;
                            _this.total = response.data.data.total;
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            //攻略栏目编码
            querySceson(){
                let _this = this;
                //使用axios向后台发请求
                axios.post('/dicgchannel/page',
                    {
                        "pageNo": 1, "pageSize": 300,
                        "data": {"name": ''}
                    })
                    .then(function (response) {
                        if (response.data.code == 200) {
                            let list = response.data.data.list;
                            for (let i = 0; i < list.length; i++) {
                                let a = {
                                    "value": list[i].code,
                                    "label": list[i].name,
                                }
                                _this.options.push(a);
                            }
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            //地域
            queryArea(){
                let _this = this;
                //使用axios向后台发请求
                axios.post('/tDicArea/queryPage',
                    {
                        "pageNo": 1, "pageSize": 300,
                        "data": {"name": ''}
                    })
                    .then(function (response) {
                        if (response.data.code == 200) {
                            let list = response.data.data.list;
                            for (let i = 0; i < list.length; i++) {
                                let a = {
                                    "value": list[i].code,
                                    "label": list[i].name,
                                }
                                _this.options1.push(a);
                            }
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            //景区
            querySenic(){
                let _this = this;
                //使用axios向后台发请求
                axios.post('/scenic/queryPage',
                {
                    "pageNo": this.currentPage,
                    "pageSize": this.pageSize,
                    "data": {"name": '', "jbcode":''}
                }//请求的参数 json格式
            )
                    .then(function (response) {
                        if (response.data.code == 200) {
                            let list = response.data.data.list;
                            for (let i = 0; i < list.length; i++) {
                                let a = {
                                    "value": list[i].code,
                                    "label": list[i].name,
                                }
                                _this.options2.push(a);
                            }
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            //实行每页有多少条的方法
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.loadData();
            },
            //实现跳转页的方法
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.loadData();
            },
            //是否包含照片 把状态数字,转换成字符
            getStatus(row, column, cellValue, index) {
                return cellValue == 0 ? "包含" : "不包含";
            },
            //查询方法
            search() {
                this.loadData();
                //搜索时,使页码为1
                this.currentPage=1;
            },
            //攻略更新的方法
            handleEdit(index, row) {
                console.log(index, row);
                //使弹出框显示
                this.dialogFormVisible = true;
                //给用户赋值
                this.title = '攻略更新';
                //点击查询携带数据
                this.form = row;
                this.imageUrl=row.gcover;
            },
            //刷新
            refresh() {
                //原始js重新请求
                location.reload();
            },
            //添加数据方法
            handleAdd() {
                //使弹出框显示
                this.dialogFormVisible = true;
                //给用户赋值
                this.title = '攻略添加';
                //点击查询携带数据
                this.form = {};
                this.imageUrl='';
            },
            //关闭方法
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {
                    });
            },
            //删除方法
            deleteDept(row) {
                this.$confirm('此操作将删除该攻略, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }).then(() => {
                    var _this = this;
                    axios.post("/car/delete/" + row.id)
                        .then(function (response) {
                            console.log(response);
                            if (response.data.code == 200) {
                                _this.$message({
                                    message: '操作成功',
                                    type: 'success'
                                });
                                _this.dialogFormVisible = false;
                                _this.loadData();
                            } else {
                                _this.$message("操作失败")
                            }
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                })
            },
            //实现用户添加或者更新
            addOrUpdate() {
                var uri = "/car/add";
                if (this.title == '攻略更新') {
                    uri = "/car/update";
                }
                var _this = this;
                axios.post(uri, this.form)
                    .then(function (response) {
                        console.log(response);
                        if (response.data.code == 200) {
                            _this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                            _this.dialogFormVisible = false;
                            _this.loadData();
                        } else {
                            _this.$message("操作失败")
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            //处理文件成功后的处理方法
            handleFileSuccess(res, file) {
                //回显上传图片
                this.imageUrl = URL.createObjectURL(file.raw);
                this.form.gcover=file.response.data.filePath;
            },
            //处理文件成功前的处理方法
            beforeFileUpload(file) {
                const FILE_NAME = file.name;
                if (
                    FILE_NAME.substring(FILE_NAME.lastIndexOf(".")) !== ".bmp" &&
                    FILE_NAME.substring(FILE_NAME.lastIndexOf(".")) !== ".jpg" &&
                    FILE_NAME.substring(FILE_NAME.lastIndexOf(".")) !== ".png"
                ) {
                    this.$message.warning("仅支持.bmp.jpg.png文件");
                    return false;
                }
                // const isJPG = file.type === 'image/jpeg';
                const isLt6M = file.size / 1024 / 1024 < 6;
                // if (!isJPG) {
                //     this.$message.error('上传头像图片只能是 JPG 格式!');
                // }
                if (isLt6M) {
                    this.file = file;
                    return true;
                }
                this.$message.warning("请上传不超过6M的文件.");
                return false;
            },
        }
    })
</script>
</body>
</html>